package pro.xxh.servlet;

import pro.xxh.JdbcUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(urlPatterns = "/xxhuser/add")
class UserAddServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //1.取值
        String name = request.getParameter("n");
        String username = request.getParameter("u");
        String id = request.getParameter("i");
        //2.储值
        if (id != null && !"null".equals(id)) {
            //存在id，执行更新
            String sql = "update `user` set `name` =?,username=? where id=?";
            JdbcUtils.modify(sql, username, name);
        } else {
            //不存在id，执行插入
            String sql = "INSERT INTO `user` (username,`PASSWORD`,`name`) VALUES (?,'789',?)";
            JdbcUtils.modify(sql, username, name);
        }

        //3.跳转
        response.sendRedirect("/xxh/user-list.jsp");
    }
}